// pages/mine/shop/card/addcoupon.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'add',
    info: null,
    pics: [],
    card_id:''
  },
  backto: function (e) {
    wx.navigateBack({})
  },

  delImg: function (e) {
    const idx = e.currentTarget.dataset.index;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.type == 'pic') {
        list[idx].pics = item.pics.splice(idx, 1);
      }
    })
    //pics.splice(idx, 1);
    this.setData({
      input_list: list
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    if (options.type == 'edit') {
      wx.setNavigationBarTitle({
        title: '编辑优惠券',
      })
      var url = 'https://www.58zltc.com/api.php/Card/getAddCouponForm/openid/' + app.globalData.openid + '/card_id/' + options.card_id + '/id/' + options.id;
    } else {
      wx.setNavigationBarTitle({
        title: '添加优惠券',
      })
      var url = 'https://www.58zltc.com/api.php/Card/getAddCouponForm/' + app.globalData.openid + '/card_id/' + options.card_id;
    }
    app.ihttpGet(url, function (res) {
      that.setData({
        input_list: res.data.form,
        add_url: res.data.add_url,
        token: token,
        card_id: options.card_id
      })
    })
  },
  bindUploadImg: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    var that = this;
    wx.chooseImage({
      count: data.num > 1 ? 9 : 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '图片上传中……',
        })
        tempFilePaths.forEach(function (item, idx) {
          wx.uploadFile({
            url: 'https://www.58zltc.com/api.php/Index/uploadImage?token=' + that.data.token,
            filePath: item,
            name: 'file',
            success: function (res) {
              var out = JSON.parse(res.data);
              console.log(out);
              if (out.err_code > 0) {
                wx.showToast({
                  title: '上传失败！',
                })
              } else {
                list.forEach(function (value, index) {
                  console.log(value);
                  console.log(data);
                  if (value.title == data.title) {
                    var pics = list[index].pics;
                    var file_name = out.data.domain;
                    if (data.num > 1) {
                      pics.push(file_name);
                    } else {
                      pics = [file_name];
                    }
                    list[index].pics = pics;
                  }
                })
                that.setData({
                  input_list: list
                })
              }

              if (idx == tempFilePaths.length - 1) {
                wx.hideLoading();
              }
              //do something
            }
          })
        })
      }
    })
  },
  formSubmit: function (e) {
    app.addFormId(e.detail.formId);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var post_data = e.detail.value;
    var data = this.data;
    let that = this;
    data.input_list.forEach(function (item, idx) {
      if (item.type == 'pic') {
        post_data[item.name] = item.pics;
        post_data.img_url = item.pics[0];
      }
    })
    if (!post_data.pics) {
      wx.showToast({
        title: '请选择优惠券图片！',
      })
      return false;
    }
    post_data.openid = app.globalData.openid;
    post_data.card_id = data.card_id;
    if (data.type == 'edit') {
      post_data.id = data.info.id;
    }
    app.ihttpPost(data.add_url, post_data, function (res) {
      if (res.data.err_code > 0) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        wx.showToast({
          title: res.data.err_msg,
          icon: 'success'
        })
        setTimeout(function () {
          wx.navigateBack({})
        }, 500)
      }
    })
  },
})